package cn.edu.suda.vote.vote;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class VoteActivity extends AppCompatActivity {

    private final static int MSG_SUCCESS = 1;
    private final static int MSG_FAILURE = 0;

    private int id;
    private String score;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                setResult(RESULT_OK);
                finish();
            } else {
                Toast.makeText(VoteActivity.this, "操作失败！请重新给分", Toast.LENGTH_SHORT).show();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vote);

        Bundle bundle = getIntent().getExtras();

        id = (int) bundle.getLong("id");

        TextView textView = (TextView) findViewById(R.id.vote_textView);
        textView.setText(bundle.getString("voter_name"));

        final EditText editText = (EditText) findViewById(R.id.vote_editText);
        score = bundle.getString("voter_score");
        editText.setText(score);
        editText.setSelection(score.length());

        Button button = (Button) findViewById(R.id.vote_button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                score = editText.getText().toString();
                if (!TextUtils.isEmpty(score)) {
                    if (Integer.valueOf(score) > 0 && Integer.valueOf(score) < 10) {
                        new Thread() {
                            @Override
                            public void run() {
                                super.run();
                                updateVoteInfo();
                            }
                        }.start();
                    } else {
                        Toast.makeText(VoteActivity.this, "请输入0~10之间的数字,不包含0和10", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }

    private void updateVoteInfo() {
        String url = "http://192.168.2.100:8080/androidServer/updateVoteInfo";
        RequestBody requestBody = new FormBody.Builder().add("viId", String.valueOf(id)).add("viScore", score).build();
        Request request = new Request.Builder().url(url).post(requestBody).build();
        OkHttpClient okHttpClient = new OkHttpClient();
        try {
            Response response = okHttpClient.newCall(request).execute();
            if (response.isSuccessful()) {
                handler.obtainMessage(MSG_SUCCESS).sendToTarget();
            } else {
                handler.obtainMessage(MSG_FAILURE).sendToTarget();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
